package com.preskool.controller;

import com.preskool.dto.ActionResult;
import com.preskool.po.Student;
import com.preskool.po.Teacher;
import com.preskool.service.UserService;
import com.preskool.utils.MailUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author kennyzzz
 * @Data 2021/1/8 10:40
 */
@RestController
@Slf4j
@Api(value = "Login接口文档")
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ActionResult loginUser(@RequestParam String username, String password, Integer rid) {
        System.out.println(username + password + rid);
        if (rid == 1) {
            Student student = (Student) userService.loginUser(username, password, rid);
            if (student != null) {
                return new ActionResult(200, "欢迎您," + student.getFulName(), student);
            } else {
                return new ActionResult(201, "用户名或者密码错误", null);
            }
        } else if (rid == 2) {
            Teacher teacher = (Teacher) userService.loginUser(username, password, rid);
            if (teacher != null) {
                return new ActionResult(200, "欢迎您," + teacher.getFulName(), teacher);
            } else {
                return new ActionResult(201, "用户名或者密码错误", null);
            }
        } else {
            if (username.equals("admin") && password.equals("admin")) {
                return new ActionResult(200, "欢迎您,管理员", null);
            } else {
                return new ActionResult(201, "用户名或者密码错误", null);
            }
        }
    }

    @PostMapping("/forgetPassword")
    public ActionResult forgetPassword(String email) {
        if (userService.selectUserByEmail(email)) {
            MailUtils.sendMail("625274188@qq.com", "你好，这是一封测试邮件，无需回复", "测试");
            return new ActionResult(200, "发送成功", null);
        }
        return new ActionResult(201, "您输入的邮箱有误", null);
    }
}
